//var urltest = 'http://192.168.0.46:8089/';//本地
//var urltest = 'http://47.92.222.216:8089/';//陕服地址测试
var urltest = 'http://wkxy.sxfu.cn:8088/FZXY_ele/';//陕服地址正式
//var urltest = 'http://47.92.222.216/electricitymeter-1/';//镐京地址测试
//var urltest = 'http://wkxy.sxhju.cn:8088/HJ_ele/'; //镐京地址正式
//包大哥的
//var urltest_b = 'http://192.168.0.60:8086/';//本地
var urltest_b = 'http://47.92.159.120:8086/';//陕服地址正式
//var urltest_b = 'http://47.92.222.216:8086/fzxy/';//陕服地址测试
//var urltest_b = 'http://47.92.159.120:8087/'; //镐京地址正式
//var urltest_b = 'http://47.92.222.216:8087/hjxy/';//镐京地址测试
sessionStorage.setItem("urltest_b", urltest_b);
sessionStorage.setItem("urltest", urltest);
var three = "";
var type = '';
var data1 = '';
$(function() {
	//判断安卓ios
	var data = '';
	//判断操作系统
	var u = navigator.userAgent,
		app = navigator.appVersion;
	var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
	var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	//	           ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端         
	if(isAndroid) {
		//这个是安卓操作系统
		//js调用app方法  getUserDataBayKeys此函数名必须与app方法名一致。	window.Android是调用Android方法。			
		window.Android.getDormInfo();
		type = 1;
	}
	if(isIOS) {　　　　 //这个是ios操作系统			
		window.webkit.messageHandlers.iosCall.postMessage({
			parameter: "dorminfo", //传递给ios(需要的key)
			callBackResult: "getValueForKey" //ios方法(取东西)之后ios判断我所需的是什么来调取回调
		});
		type = 2;
	}
})
var ret = '';
function parseQueryString(url) { 
	var reg_url = /^[^\?]+\?([\w\W]+)$/,
		  reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
		  arr_url = reg_url.exec(url),
		  ret = {}; 
	if(arr_url && arr_url[1]) {  
		var str_para = arr_url[1],
			result;  
		while((result = reg_para.exec(str_para)) != null) {   
			ret[result[1]] = result[2];  
		} 
	} 
	return ret;
}
//区域改变，楼层列表显示
$("#sel_1").change(function() {
	var str2_2 = '<option value="0000">' + "请选择宿舍号" + '</option>';
	$("#sel_2").html("");
	$("#sel_3").html(str2_2);
	var aaaa = $("#sel_1 option:checked").html();
	var obj = {
		"area": aaaa
	};
	var req = JSON.stringify(obj);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + "/notcheck/roomElectricityMeter",
		data: req,
		async: false,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			three = msg.obj.floorRoomElectricityMeterMap;
			if(msg.resCode == 200) {
				//					alert(msg.resmsg)
				$("#sel_2").html("");
				console.log(msg.obj);
				var str = '<option value="0000">' + "请选择楼层" + '</option>';
				for(var i = 0; i < msg.obj.floorList.length; i++) {
					str += "<option value=" + i + ">" + msg.obj.floorList[i] + "</option>";
				}
				$("#sel_2").append(str);
			} else if(msg.resCode == 404) {
				window.location.href = "jq404.html";
			} else {
				//				alert(msg.resmsg);
				$("#tips").html(msg.resmsg);
				$('#myModal_tips').modal('show');
			}
		},
		error: function() {
			//			alert(msg.resmsg);
			$("#tips").html(msg.resmsg);
			$('#myModal_tips').modal('show');
		}
	});
})
//楼层改变，宿舍号列表显示
$("#sel_2").change(function() {
	$("#sel_3").html("");
	console.log(three);
	var aaaa = $("#sel_2 option:checked").html();
	var str = '<option value="0000">' + "请选择宿舍号" + '</option>';
	var sttrr = three[aaaa];
	for(var i = 0; i < sttrr.length; i++) {
		str += "<option value=" + sttrr[i].meterId + ">" + sttrr[i].roomId + "</option>";
	}
	$("#sel_3").append(str);

})

$('#query_search').click(function() {
	var check = $("#sel_3 option:checked").html();
	var check1 = $("#sel_2 option:checked").html();
	var check2 = $("#sel_1 option:checked").html();
	var card = $('#shenfenzheng').val();
	if(check != "请选择宿舍号" && check1 != "请选择楼层" && check2 != "请选择区域") {
		isCardNo(card);
	} else {
		//		alert("请先选择您的宿舍号")
		$("#tips").html("请先选择您的宿舍号");
		$('#myModal_tips').modal('show');
	}

	function isCardNo(card) {
		sessionStorage.setItem("ID_card", $("#shenfenzheng").val());
		var address = {
			"sel_1": $("#sel_1 option:checked").html(),
			"sel_2": $("#sel_2 option:checked").html(),
			"sel_3": $("#sel_3 option:checked").html(),
			"sel_4": $("#sel_3 option:checked").val(),
		};
		var address = JSON.stringify(address);
		sessionStorage.setItem("address", address);
		console.log(address)
		if(data1.SFZ != '') {
			sessionStorage.setItem("ID_card", data1.SFZ);
			//			alert(sessionStorage.getItem("ID_card"))
			window.location.href = 'smart_meter.html';
		} else {
			sessionStorage.setItem("ID_card", data1.SFZ);
			window.location.href = "pay_wx_ali.html";
		}
	}
})
$('#smart_jilu').click(function() {
	if(data1.SFZ==''){
		$("#tips").html("您属于游客身份,无法查询到您的充值记录");
		$('#myModal_tips').modal('show');
	}else{
		location.href = "Recharge_record.html"
	}	
});

function getDormInfoResult(data) {
	console.log(data);
	var r = window.location;
	var r = decodeURI(decodeURI(r));
	data1 = parseQueryString(r);
	console.log(data1.SFZ);
	if(data == "" || data == null || data == undefined) {
		var address_add = '';
	} else {
		if(type == 1) {
			var address_add = data;
		} else {
			var address_add = JSON.parse(data);
		}
	}
	if(address_add != '') {
		var address = {
			"sel_1": address_add.buildname, //区域
			"sel_2": address_add.floor, //楼层
			"sel_3": address_add.roomid, //宿舍号
			"sel_4": address_add.meterid, //电表号
		};
		var address = JSON.stringify(address);
		sessionStorage.setItem("address", address);
		if(data1.SFZ != '') {
			var ID_card = data1.SFZ;
			sessionStorage.setItem("ID_card", ID_card);
		} else {
			var ID_card = null;
			sessionStorage.setItem("ID_card", ID_card);
		}
		window.location.href = "smart_meter.html";
		return;
	} else {
		if(data1.SFZ == '') {
			var ID_card = null;
			sessionStorage.setItem("ID_card", ID_card);
		} else {
			var ID_card = data1.SFZ;
			sessionStorage.setItem("ID_card", ID_card);

		}
	}

	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "GET",
		url: urltest + "/notcheck/getEreaInfo",
		data: "",
		async: false,
		dataType: "json",
		beforeSend: function() {
			$("#showResult").show();
		},
		success: function(msg) {
			//			console.log(msg);
			if(msg.resCode == 200) {
				$("#showResult").remove();
				//					alert(msg.resmsg)
				$("#sel_1").html("");
				$("#sel_2").html("");
				$("#sel_3").html("");
				var pp = sessionStorage.getItem("area");
				var pp1 = sessionStorage.getItem("floor");
				var pp2 = sessionStorage.getItem("roomid");
				if(pp == undefined || pp == null || pp == "" || pp == 'undefined') {
					pp = "请选择区域"
				}
				if(pp1 == undefined || pp1 == null || pp1 == "" || pp1 == 'undefined') {
					pp1 = "请选择楼层"
				}
				if(pp2 == undefined || pp2 == null || pp2 == "" || pp2 == 'undefined') {
					pp2 = "请选择宿舍号"
				}
				var str = '<option value="0000">' + pp + '</option>';
				var str1 = '<option value="0000">' + pp1 + '</option>';
				var str2 = '<option value="0000">' + pp2 + '</option>';
				for(var i = 0; i < msg.obj.area.length; i++) {
					str += "<option value=" + i + ">" + msg.obj.area[i] + "</option>";
				}
				$("#sel_1").append(str);
				$("#sel_2").append(str1);
				$("#sel_3").append(str2);
			} else if(msg.resCode == 404) {
				window.location.href = "jq404.html";
			} else {				
				//				alert(msg.resmsg);
				$("#tips").html(msg.resmsg);
				$('#myModal_tips').modal('show');
			}
		},
		error: function() {
			//			alert(msg.resmsg);
			$("#tips").html(msg.resmsg);
			$('#myModal_tips').modal('show');
			$("#showResult").remove();
		}
	});

}